/**
 * ajax 请求封装工具类
 */
import config from './config'

const request = {
	get: function(url, data) {
		const settings = {
			url: url,
			data: data,
			method: 'GET',
		}
		if (typeof(url) === 'object') {
			settings = Object.assign({}, url)
			settings['method'] = 'GET'
		}
		return request.ajax(settings)
	},
	post: function(url, data) {
		const settings = {
			url: url,
			data: data,
			method: 'POST'
		}
		if (typeof(url) === 'object') {
			settings = Object.assign({}, url)
			settings['method'] = 'POST'
		}
		return request.ajax(settings)
	},
	put: function(url, data) {
		const settings = {
			url: url,
			data: data,
			method: 'PUT'
		}
		if (typeof(url) === 'object') {
			settings = Object.assign({}, url)
			settings['method'] = 'PUT'
		}
		return request.ajax(settings)
	},
	delete: function(url, data) {
		const settings = {
			url: url,
			data: data,
			method: 'DELETE'
		}
		if (typeof(url) === 'object') {
			settings = Object.assign({}, url)
			settings['method'] = 'DELETE'
		}
		return request.ajax(settings)
	},
	ajax: function(settings) {
		if (settings && settings.header) {
			settings.header[config.TOKEN_NAME] = settings.header[config.TOKEN_NAME] || uni.getStorageSync(config.TOKEN_NAME);
			settings.header['content-type'] = settings.header['content-type'] || 'application/json;charset=utf-8';
		}
		return new Promise(function(resolve, reject) {
			//获取存储的服务器配置信息
			const serverInfo = uni.getStorageSync(config.SERVER_INFO_CONFIG_KEY);
			uni.request({
				url: serverInfo['serverUrl'] + settings.url,
				data: settings.data,
				dataType: settings.dataType || 'json',
				method: settings.method || 'GET',
				responseType: settings.responseType || 'json',
				header: settings.header || {
					'x-token': uni.getStorageSync(config.TOKEN_NAME),
					'content-type': 'application/json;charset=utf-8'
				},
				fail: (res) => {
					uni.showToast({
						icon: 'none',
						title: '网络连接失败\r\n请检查服务器配置信息',
						duration: 2000
					});
				},
				success: (res) => {
					//如果未登录，那么直接去到登录页面
					if (res.data && res.data.code === 9999) {
						uni.removeStorageSync(config.TOKEN_NAME);
						uni.reLaunch({
							url: "/pages/login/login"
						})
					}
					resolve(res.data)
				}
			});
		})
	},
	upload: function(url, settings) {
		if (!settings.header) {
			settings['header'] = {}
		}
		if (!url) {
			url = settings.url
		}
		const serverInfo = uni.getStorageSync(config.SERVER_INFO_CONFIG_KEY);
		settings.url = serverInfo['serverUrl'] + url
		settings.header[config.TOKEN_NAME] = settings.header[config.TOKEN_NAME] || uni.getStorageSync(config.TOKEN_NAME);
		return uni.uploadFile(settings)
	},
	download: function(url, settings) {
		if (!settings.header) {
			settings['header'] = {}
		}
		const serverInfo = uni.getStorageSync(config.SERVER_INFO_CONFIG_KEY);
		if (!url) {
			url = serverInfo['serverUrl'] + settings.url
		}
		settings.url = url || serverInfo['serverUrl'] + settings.url
		settings.header[config.TOKEN_NAME] = settings.header[config.TOKEN_NAME] || uni.getStorageSync(config.TOKEN_NAME);
		return uni.downloadFile(settings)
	}
}
export default request
